package gov.pr.celepar.sentinela.admin.action.consultas;

import gov.pr.celepar.sentinela.admin.action.BaseAction;
import gov.pr.celepar.sentinela.admin.excecao.SentinelaServerException;
import gov.pr.celepar.sentinela.admin.facade.AuditoriaControleFacade;
import gov.pr.celepar.sentinela.admin.form.AuditoriaForm;
import gov.pr.celepar.sentinela.admin.pojo.AuditoriaControle;
import gov.pr.celepar.framework.util.Pagina;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class AtivacaoAuditoriaReportAction extends BaseAction {

	private static Log logger = LogFactory
			.getLog(AtivacaoAuditoriaReportAction.class);

	public String executar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.forward = "lista_auditoria";
		try {
			AuditoriaForm auditForm = (AuditoriaForm) form;

			this.listarSistemasAutorizados(request);

			if (auditForm.getCodSistema() != null
					&& !auditForm.getCodSistema().equals("")) {
				Long codSistema = Long.decode(auditForm.getCodSistema());
				logger.debug("Pegando informações de auditoria");

				Integer indicePagina = new Integer(
						auditForm.getIndice() == null ? "1" : auditForm
								.getIndice());

				Collection<AuditoriaControle> dados = AuditoriaControleFacade
						.listarBySistema(codSistema);

				if (dados != null && !dados.isEmpty()) {
					Integer totalRegistros = new Integer(dados.size());
					Pagina pagina = new Pagina(totalRegistros, indicePagina,
							totalRegistros);
					pagina.setRegistros(dados);
					request.setAttribute("pagina", pagina);
				} else {
					throw new SentinelaServerException(
							"Nenhum registro encontrado! ");
				}
			}
		} catch (SentinelaServerException appEx) {
			throw appEx;
		} catch (Exception e) {
			throw e;
		}
		return "lista_auditoria";
	}
}
